package student.test.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import student.test.Service.StudentService;
import student.test.entity.Student;
import student.test.util.GeneralResponse;
import java.util.ArrayList;
import java.util.List;
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping("/students")
    public GeneralResponse students(){
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.getAllStudents());
        return response;
    }
    @PostMapping("/student/add")
    public GeneralResponse add(@RequestParam String name, @RequestParam String sex,@RequestParam int age,@RequestParam String point){
        Student student = new Student();
        student.setSex(sex);
        student.setName(name);
        student.setAge(age);
        student.setPoint(point);
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.addStudent(student));
        return  response;
    }
    @PutMapping("/student/update")
    public GeneralResponse update(@RequestParam String name,@RequestParam String sex,@RequestParam int id,@RequestParam int age, @RequestParam String point){
        Student student = new Student();
        student.setSex(sex);
        student.setName(name);
        student.setAge(age);
        student.setPoint(point);
        student.setId(id);
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.updateStudent(student));
        return  response;
    }
    @PostMapping("/student/delete")
    public GeneralResponse delete(@RequestParam int id){
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.deleteStudentById(id));
        return  response;
    }
    @GetMapping("/student")
    public GeneralResponse studentById(@RequestParam int id){
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        List<Student> result = new ArrayList<>();
        result.add(studentService.getStudentById(id));
        response.setData(result);
        return  response;
    }
}
